package com.libbase.kotlin.presenter

import com.base.kotlin.mvp.BasePresenter
import com.libbase.kotlin.NewModel
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

class NewPresenter : BasePresenter<NewContract.View, NewContract.Model>(), NewContract.Presenter {

    override fun getNews(nav: String) {
        if (isViewAttached()) {
            mView?.showLoading()
            try {
                val disposable = mModel?.getNews(nav)?.subscribeOn(Schedulers.io())?.observeOn(AndroidSchedulers.mainThread())?.subscribe { result ->
                    mView?.hideLoading()
                    mView?.onNewsSuccess(result)
                }
                disposable?.let {
                    mDisposables.add(it)
                }
            } catch (e: RuntimeException) {
                mView?.hideLoading()
                mView?.onError(e)
            }

        }
    }

    override fun getModel(): NewContract.Model? {
        return NewModel()
    }


}